// @flow
import React, { Component } from 'react'
import styles from './SmsSettingActions.css'
import { Button, Popconfirm, message } from 'antd'
import universalFetch, { handleFetchError } from 'store/modules/fetch'

type Props = {
  refreshSmsSettings: Function,
  data: Object,
  history: Object,
  current: number,
  pageSize: number
}
class SmsSettingActions extends Component {
  props: Props
  deleteSmsSetting: Function
  constructor (props: Props) {
    super(props)
    this.deleteSmsSetting = this.deleteSmsSetting.bind(this)
  }

  deleteSmsSetting () {
    const { data, current, pageSize } = this.props
    universalFetch(`${__SERVICE_API__}sys/sms/${data.id}`, {
      method: 'DELETE'
    })
    .then((res) => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        throw new Error(JSON.stringify({ code: json.code, message: json.message }))
      }
      this.props.refreshSmsSettings(current, pageSize)
      message.success('删除成功')
    })
     .catch((error) => {
       handleFetchError(error)
       console.error(error)
       message.error('删除失败')
     })
  }
  render () {
    const { history, data } = this.props
    return (
      <div className={styles['actions']}>
        <Button
          size='small'
          type='primary'
          style={{ marginRight: '10px' }}
          onClick={() => {
            history.push({
              pathname: '/system/sms-edit',
              state: {
                data
              } })
          }}
          >
          编辑
        </Button>
        <Popconfirm title='确定要删除吗'
          arrowPointAtCenter
          placement='left'
          onConfirm={this.deleteSmsSetting}
          >
          <Button
            size='small'
            type='danger'
            >
            删除
          </Button>
        </Popconfirm>
      </div>
    )
  }
}

export default SmsSettingActions
